package com.aiju.support.file;

import com.aiju.utils.L;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: 佐为
 * @date: 2016-11-18 14:22
 * @version: V1.0
 * @Description: 描述
 */
public class FileVisitorImage implements FileTreeVisitor {

    private List<String> images;
    /**
     * 加载图片类型
     */
    private String png = "png";
    private String img = "jpg";

    private boolean first = false;
    private File root;


    private Ipic ipic;

    public void setIpic(Ipic ipic) {
        this.ipic = ipic;
    }

    public interface Ipic {
        void onPic(List<String> image);
    }

    public FileVisitorImage() {
        images = new ArrayList<>();
    }

    @Override
    public void preVisitDirectory(File directory) {
        if (!first) {
            root = directory;
            first = true;
            L.d("扫描开始");
        }
    }

    @Override
    public void visitFile(File file) {
        String fileName = file.getAbsolutePath();
        String prefix = fileName.substring(fileName.lastIndexOf(".") + 1);
        if (prefix.equals(png) || prefix.equals(img)) {
            L.d(file.getAbsolutePath());
            images.add(file.getAbsolutePath());
        }

    }

    @Override
    public void postVisitDirectory(File directory) {
        if (first && root != null) {
            if (root == directory) {
                L.d("扫描完成" + images.size());
                if (ipic != null) {
                    ipic.onPic(images);
                }
            }
        }
    }
}
